﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalconDotNet;

namespace Kimd.Vision
{
    [Serializable]
    public class PointNB : ShapeNB
    {
        public PointF Center;
        public float CrossSize;

        public PointNB()
            : base()
        {
            CrossSize = 16;
        }
        public override void Draw(HWindow window, Graphics g, Func<PointF, PointF> pixel2Window, Func<float, float> lenConvert, bool disableEdit)
        {
            window.SetColor(PaintColor);
            window.SetLineWidth(LineWidth);
            window.SetDraw("margin");
            float size = Math.Min(lenConvert(CrossSize), 100);
            window.DispCross(new HTuple(Center.Y), new HTuple(Center.X), size, 0);
        }
        public override HObject ToRegion()
        {
            HObject l1 = null;
            HObject l2 = null;
            HObject cross = null;
            HOperatorSet.GenRegionLine(out l1, new HTuple(Center.Y - CrossSize / 2), new HTuple(Center.X), new HTuple(Center.Y + CrossSize / 2), new HTuple(Center.X));
            HOperatorSet.GenRegionLine(out l2, new HTuple(Center.Y), new HTuple(Center.X - CrossSize / 2), new HTuple(Center.Y), new HTuple(Center.X + CrossSize / 2));
            HOperatorSet.Union2(l1, l2, out cross);
            return cross;
        }

        public override void Change(PointF point)
        {
            switch (ChangeModel)
            {
                case PositionNB.In:
                    float dx = point.X - Center.X;
                    float dy = point.Y - Center.Y;
                    this.Center = new PointF(Center.X + dx, Center.Y + dy);
                    break;
            }
        }

        public override PositionNB PointOnShape(PointF testPoint, Func<float, float> lenConvert)
        {
            double dcenter = HMisc.DistancePp(new HTuple(testPoint.Y), new HTuple(testPoint.X), new HTuple(Center.Y), new HTuple(Center.X));
            distance2Handle = (int)lenConvert(10);
            if (dcenter < distance2Handle)
                return PositionNB.In;
            return PositionNB.Out;
        }
    }
}
